package com.example.testnews.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.example.testnews.activity.NewsActivity;
import com.example.testnews.R;
import com.example.testnews.bean.News;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private SwipeRefreshLayout swipeRefreshLayout;
    private static final String TAG = "HomeAdapter";
    private static OnItemClickListener mListener;
    private  List<News> newsList = new ArrayList<>();
    public static   List<News> collectNewsList = new ArrayList<>();
    private News collectNews;
    //private OnItemClickListener mListener;
    private Context context;


    public HomeAdapter(Context context) {
        this.context = context;
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,parent,false);
        return new NewsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        News news = newsList.get(position);

        NewsViewHolder newsViewHolder = (NewsViewHolder) holder;
        Log.d(TAG, "onBindViewHolder: "+news);
        Glide.with(newsViewHolder.mIvIcon.getContext()).load(news.picUrl.replace("http","https")).into(newsViewHolder.mIvIcon);

        newsViewHolder.mTvTitle.setText(news.title);
        newsViewHolder.mTvDesc.setText(news.description);
        newsViewHolder.mTvTime.setText(news.time);

        newsViewHolder.mNewsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NewsActivity.class);
                intent.putExtra("extra_data",newsList.get(position).url);
                context.startActivity(intent);

//                if (mListener != null){
//                    mListener.onClick(position);
//                }
            }
        });

        newsViewHolder.mNewsView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                collectNews = newsList.get(position);
                collectNewsList.add(collectNews);
                Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
                return true;
            }
        });


    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    public void setNews(List<News> newsList){
        Log.e(TAG, "setNews: "+newsList);
        this.newsList=newsList;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void onClick(int position);
    }

    public static void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }


    static class NewsViewHolder extends RecyclerView.ViewHolder{

        public ImageView mIvIcon;
        public TextView mTvTitle;
        public TextView mTvDesc,mTvTime;
        public View mNewsView;


        public NewsViewHolder(View view){
            super(view);
            mIvIcon =view .findViewById(R.id.icon);
            mTvTitle =view .findViewById(R.id.title);
            mTvDesc =view .findViewById(R.id.desc);
            mTvTime =view .findViewById(R.id.time);
            mNewsView = view;

        }
    }
}
